/**
 * projectName: palmdoctor-mb-backend
 * fileName: BaseServiceImpl.java
 * packageName: com.palmdoctor.mb.framework
 * date: 2021-05-07 15:46
 * copyright(c) 2021 http://www.hydee.cn/ Inc. All rights reserved.
 */
package com.zny.sescore.service;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zny.sescore.model.BaseModel;
import com.zny.sescore.util.RequestUtil;

import java.io.Serializable;

/**
 * @version: V1.0
 * @author: zhangnayi
 * @className: BaseServiceImpl
 * @packageName: com.palmdoctor.mb.framework
 * @description:
 * @data: 2021-05-07 15:46
 **/
public class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseModel<T>> extends ServiceImpl<M, T> {

    @Override
    public boolean save(T entity) {
        Integer loginUserId = RequestUtil.getLoginUserId();
        entity.setCreateUser(loginUserId);
        entity.setUpdateUser(loginUserId);
        return super.save(entity);
    }

    @Override
    public boolean updateById(T entity) {
        if (RequestUtil.isLoginIn()) {
            entity.setUpdateUser(RequestUtil.getLoginUserId());
        }
        return super.updateById(entity);
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        Class<?> cls = entity.getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
        Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
        if (StringUtils.checkValNull(idVal)) {
            Integer loginUserId = RequestUtil.getLoginUserId();
            entity.setCreateUser(loginUserId);
            entity.setUpdateUser(loginUserId);
            return this.save(entity);
        } else {
            entity.setUpdateUser(RequestUtil.getLoginUserId());
            return this.updateById(entity);
        }
    }

    /**
     * 根据主键逻辑删除
     *
     * @param entity 数据库实体类
     * @return boolean
     * @author ly
     * @date 2021/7/1 0001
     */
    public boolean removeById(T entity) {
        if (entity == null) {
            return false;
        }
        Class<?> cls = entity.getClass();
        TableInfo tableInfo = TableInfoHelper.getTableInfo(cls);
        Assert.notNull(tableInfo, "error: can not execute. because can not find cache of TableInfo for entity!", new Object[0]);
        String keyProperty = tableInfo.getKeyProperty();
        Assert.notEmpty(keyProperty, "error: can not execute. because can not find column for id from entity!", new Object[0]);
        Object idVal = ReflectionKit.getFieldValue(entity, tableInfo.getKeyProperty());
        if (StringUtils.checkValNull(idVal)) {
            return false;
        } else {
            return super.removeById(entity);
        }
    }

    @Override
    public boolean removeById(Serializable id) {
        if (id == null) {
            return false;
        }
        return super.removeById(id);
    }

}